<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('admin_model', 'adminManager');
        $this->user = ($this->adminManager->is_logged()) ? $this->adminManager->get_admin($this->session->userdata('email')) : false;
        $this->view_folder = strtolower(__CLASS__) . '/';
    }

    public function index() {
        if (!$this->adminManager->is_logged()) {
            redirect('admin/login');
            exit;
        }
        // On load le modèle article
        $this->load->model('article_model', 'articleManager');
        // On récupère les articles
        $articles = $this->articleManager->getAll();
        $data = array(
            'title' => 'Panel d\'admin',
            'user' => $this->user,
            'articles' => $articles,
            'content' => $this->view_folder . __FUNCTION__
        );
        $this->load->view('template/content', $data);
    }

    public function login() {
        if ($this->adminManager->is_logged()) {
            redirect('admin');
            exit;
        }

        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('password', 'Mot de passe', 'trim|required');

        if ($this->form_validation->run()) {
            if ($this->adminManager->login($this->input->post('email'), $this->input->post('password'))) {
                redirect('admin');
                exit;
            } else {
                $this->session->set_flashdata('error', 'Mauvais identifiants');
                redirect(current_url());
                exit;
            }
        }

        $data = array(
            'title' => 'Connexion',
            'content' => $this->view_folder . __FUNCTION__
        );
        $this->load->view('template/content', $data);
    }

    public function logout() {
        $this->session->sess_destroy();
        redirect();
        exit;
    }

    /*
     * Partie article
     */

    public function insertarticle() {
        // On load le modèle
        $this->load->model('article_model', 'articleManager');

        $this->form_validation->set_rules('title', 'Titre', 'trim|required');
        $this->form_validation->set_rules('description', 'Description', 'trim|required');
        $this->form_validation->set_rules('price', 'Prix', 'trim|required');

        if ($this->form_validation->run()) {
            $datas = array('title' => $this->input->post('title'), 'description' => $this->input->post('description'), 'price_amount' => $this->input->post('price'));
            $this->articleManager->insert($datas);
            redirect('admin');
            exit;
        }

        $data = array(
            'title' => 'Edit d\'un article',
            'user' => $this->user,            
            'content' => $this->view_folder . __FUNCTION__
        );
        $this->load->view('template/content', $data);
    }
    
    public function editarticle($article_id) {
        // On load le modèle
        $this->load->model('article_model', 'articleManager');
        // On récupère les articles
        $article = $this->articleManager->get((int) $article_id);

        $this->form_validation->set_rules('title', 'Titre', 'trim|required');
        $this->form_validation->set_rules('description', 'Description', 'trim|required');
        $this->form_validation->set_rules('price', 'Prix', 'trim|required');

        if ($this->form_validation->run()) {
            $datas = array('title' => $this->input->post('title'), 'description' => $this->input->post('description'), 'price_amount' => $this->input->post('price'));
            $this->articleManager->update($article_id, $datas);
            redirect('admin');
            exit;
        }

        $data = array(
            'title' => 'Edit d\'un article',
            'user' => $this->user,
            'article' => $article,
            'content' => $this->view_folder . __FUNCTION__
        );
        $this->load->view('template/content', $data);
    }

    public function deletearticle($article_id) {
        if (!$this->adminManager->is_logged()) {
            redirect('article');
            exit;
        }
        // On load le modèle
        $this->load->model('article_model', 'articleManager');
        // on delete les articles
        $this->articleManager->delete((int) $article_id);
        redirect('admin');
    }

}
